package com.wpre.springblog;

import jakarta.servlet.http.HttpSession;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: LianBao
 * Date: 2024-10-24
 * Time: 11:56
 */

@RestController
@RequestMapping("/user")
public class LoginController {
    @RequestMapping("/login")
    public boolean login(@RequestParam("userName") String userName, @RequestParam("password") String password) {
        //@RequestParam表示必传参数
        if (!StringUtils.hasLength(userName) || !StringUtils.hasLength(password)) {
            return false;
        }
        //如果用户名和密码都正确,返回true
        return "admin".equals(userName) && "admin".equals(password);
    }

    @RequestMapping("getLoginUser")
    public String getLoginUser(HttpSession session) {
        String userName = (String) session.getAttribute("userName");
        if (!StringUtils.hasLength(userName)) {
            return "";
        }
        return userName;
    }

}
